package com.hnust.controller;

import com.hnust.domain.record.AuditJoinRecord;
import com.hnust.domain.record.AuditRegisterRecord;
import com.hnust.service.AuditService;
import com.hnust.service.TokenService;
import com.hnust.util.DataResult;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 创建人：gh
 * 创建时间：2020/10/6
 * 描述：审核注册、审核加入
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/audit")
public class AuditController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AuditService auditService;

    // 点击审核加入时，返回审核信息
    @GetMapping("/join/get")
    public Result<DataResult<List<AuditJoinRecord>>> auditJoinGet(
            @RequestParam("token")String token,
            @RequestParam("currentPage")Integer currentPage,
            @RequestParam("rows")Integer rows,
            @RequestParam("collegeId") String collegeId
    ){
        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            return new Result<>(1, "获取返回审核信息成功", new DataResult<>(auditService.auditJoinGet(currentPage, rows, collegeId)));
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

    // 通过/驳回加入申请
    @GetMapping("/join/operate")
    public Result auditJoinOperate(
            @RequestParam("token")String token,
            @RequestParam("auditJoinId")String auditJoinId,
            @RequestParam("result")Integer result
    ){
        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(auditService.auditJoinOperate(auditJoinId, result)){
                return new Result<>(1, "操作成功", null);
            }else{
                return new Result<>(0, "操作失败", null);
            }
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

    // 批量通过/驳回加入申请
    @PostMapping("/join/operate")
    public Result auditBatchJoinOperate(@RequestBody Map<Object,Object> map
    ){
        String token=map.get("token").toString();
        JSONArray auditJoinIds= JSONArray.fromObject(map.get("auditJoinId"));
        Integer result=(Integer) map.get("result");

        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(auditService.auditBatchJoinOperate(auditJoinIds, result)){
                return new Result<>(1, "操作成功", null);
            }else{
                return new Result<>(0, "操作失败", null);
            }
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

    // 点击审核注册时，返回审核信息
    @GetMapping("/register/get")
    public Result<DataResult<List<AuditRegisterRecord>>> auditRegisterGet(
            @RequestParam("token")String token,
            @RequestParam("currentPage")Integer currentPage,
            @RequestParam("rows")Integer rows,
            @RequestParam("collegeId") String collegeId
    ){
        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            return new Result<>(1, "获取审核信息成功", new DataResult<>(auditService.auditRegisterGet(currentPage, rows, collegeId)));
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

    // 通过注册/驳回注册
    @GetMapping("/register/operate")
    public Result auditRegisterOperate(
            @RequestParam("token")String token,
            @RequestParam("auditRegisterId")String auditRegisterId,
            @RequestParam("result")Integer result
    ){
        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(auditService.auditRegisterOperate(auditRegisterId, result)){
                return new Result<>(1, "操作成功", null);
            }else{
                return new Result<>(0, "操作失败", null);
            }
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

    // 批量通过注册/批量驳回注册
    @PostMapping("/register/operate")
    public Result auditBatchRegisterOperate(@RequestBody Map<Object,Object> map)
    {
        String token=map.get("token").toString();
        JSONArray auditRegisterIds= JSONArray.fromObject(map.get("auditRegisterId"));
        Integer result=(Integer) map.get("result");

        // 验证用户的token是否正确
        if(tokenService.verifyToken(token,"院管理员")){
            if(auditService.auditBatchRegisterOperate(auditRegisterIds, result)){
                return new Result<>(1, "操作成功", null);
            }else{
                return new Result<>(0, "操作失败", null);
            }
        }
        return new Result<>(0, "没有操作权限,请登录", null);
    }

}
